define(['url','template','jquery','cookie'],(url,template) =>{
    class Header {
        constructor () {
            this.info().then(() =>{
                this.hover()
                this.changeNum()
                this.auto()
                this.exit()
                this.isLogin()
            })
            this.renderTab().then(() =>{
                
            })
        }
        info () {
            return new Promise(resolve => {
                $('header').load('/html/header.html',resolve)
            })
        }
        hover () {
            $('#mine').hover(() =>{
                $('#mineshop').css('display','block')
            },() =>{
                $('#mineshop').css('display','none')
            })
            $('#client').hover(() =>{
                $('#shopcar').css('display','block')
            },() =>{
                $('#shopcar').css('display','none')
            })
            $('#mineshop').hover(() =>{
                $('#mineshop').css('display','block')
            },() =>{
                $('#mineshop').css('display','none')
            })
            $('#shopcar').hover(() =>{
                $('#shopcar').css('display','block')
            },() =>{
                $('#shopcar').css('display','none')
            })
        }
        renderTab () {
            return new Promise(resolve => {
                $.get(`${url.xmyxUrl}/tabs`,resp =>{
                    let  {list} = resp.data
                    list = list.slice(1,8)
                    var str = template('header-nav',{list})
                    $('#b-nav').html(str)
                })
                resolve()
            })
        }
        changeNum () {
            const name = $.cookie('username')
            if(name){
                let cart = localStorage.getItem('cart')
                cart = JSON.parse(cart)
                if(cart){
                    var num = cart.reduce((count,shop) =>{
                        count += shop.count
                        return count
                    },0)
                    $('.pic-2 a span').html(num)
                    $('.a-car-num').html(num)
                }
            }
        }
        auto () {
            var coo = $.cookie('username')
            if(coo){
                $('.start').hide()
                $('.end').show()
                $('.end .h-name').html(coo)
            }
        }
        exit () {
            $('.h-exit').on('click',() =>{
                $.removeCookie('username')
                $('.start').show()
                $('.end').hide()
                $('.myorder').attr('href','javascript:;')
                $('.mycoupons').attr('href','javascript:;')
                $('.mysite').attr('href','javascript:;')
                $('.mycar').attr('href','javascript:;')
                header.changeNum()  
            })
        }
        isLogin () {
            var username = $.cookie('username')
            if(username){
                $('.myorder').attr('href','/html/myOrder.html')
                $('.mycoupons').attr('href','/html/coupons.html')
                $('.mysite').attr('href','/html/mySite.html')
                $('.mycar').attr('href','/html/shopCar.html')
            }else{
                $('.myorder').on('click',() =>{
                    $('#login').modal('show')
                })
                $('.mycoupons').on('click',() =>{
                    $('#login').modal('show')
                })
                $('.mysite').on('click',() =>{
                    $('#login').modal('show')
                })
                $('.mycar').on('click',() =>{
                    $('#login').modal('show')
                })
                $('.mymall').on('click',() =>{
                    $('#login').modal('show')
                })
            }
        }
    }
    return new Header()
})
